﻿using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using TodoMVC.Models.BusinessModels;
using TodoMVC.Tools;

namespace TodoMVC.Models.DBModels
{
    /// <summary>
    /// 代办事项行为状态
    /// </summary>
    public enum TodoItemStatus
    {
        /// <summary>
        /// 待办中
        /// </summary>
        Active = 1,
        /// <summary>
        /// 已完成
        /// </summary>
        Completed = 2
    }
    /// <summary>
    /// 代办事项
    /// </summary>
    public class TodoItem
    {
        public TodoItem()
        {
            var now = DateTime.Now;
            this.UpdateTime = now;
            //避免漏赋值
            if (this.CreateTime == DateTime.MinValue)
            {
                this.CreateTime = now;
            }
        }

        /// <summary>
        /// 唯一数字id
        /// </summary>
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public long Id { get; set; }

        /// <summary>
        /// 唯一guid,需要手动分配
        /// </summary>
        [DatabaseGenerated(DatabaseGeneratedOption.None)]
        public Guid Guid { get; set; }

        /// <summary>
        /// 代办事项内容
        /// </summary>
        [Required]
        [StringLength(maximumLength: 2000, MinimumLength = 1)]
        public string Content { get; set; }

        /// <summary>
        /// 待办项状态
        /// </summary>
        public TodoItemStatus TodoItemStatus { get; set; } = TodoItemStatus.Active;

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime UpdateTime { get; set; }

        /// <summary>
        /// 完成时间
        /// </summary>
        public DateTime CompleteTime { get; set; }

        /// <summary>
        /// 是否已删除
        /// </summary>
        [DefaultValue(false)]
        public bool IsDeleted { get; set; }

        public TodoItemResult ToTodoItemResult()
        {
            return new TodoItemResult()
            {
                Id = this.Id,
                Guid = this.Guid,
                Content = this.Content,
                CompleteTime = this.CompleteTime.ToString(DateTimeHelper.CommonFormat),
                CreateTime = this.CreateTime.ToString(DateTimeHelper.CommonFormat),
                UpdateTime = this.UpdateTime.ToString(DateTimeHelper.CommonFormat),
                IsDeleted = this.IsDeleted,
                TodoItemStatus = this.TodoItemStatus
            };
        }
    }
}
